Bars
Contents
2. Bars¶
2.1. 概要¶
2.3. MADB Labを用いた作図例¶
2.3.1. 下準備¶
import pandas as pd
import plotly.express as px
import warnings
warnings.filterwarnings('ignore')
# 前処理の結果,以下に分析対象ファイルが格納されていることを想定
PATH_DATA = '../../data/preprocess/out/magazines.csv'
# Jupyter Book用のPlotlyのrenderer
RENDERER = 'plotly_mimetype+notebook'
def add_years_to_df(df, unit_years=10):
"""unit_years単位で区切ったyears列を追加"""
df_new = df.copy()
df_new['years'] = \
pd.to_datetime(df['datePublished']).dt.year \
// unit_years * unit_years
df_new['years'] = df_new['years'].astype(str)
return df_new
def show_fig(fig):
"""Jupyter Bookでも表示可能なようRendererを指定"""
fig.update_layout(margin=dict(t=50, l=25, r=25, b=25))
fig.show(renderer=RENDERER)
df = pd.read_csv(PATH_DATA)
2.3.2. 雑誌別の合計作品数¶
col_count = 'cname'
df_plot = \
df.groupby('mcname')[col_count].nunique().reset_index()
df_plot['ratio'] = df_plot[col_count] / df_plot[col_count].sum()
df_plot['years'] = '全期間'
fig = px.bar(
df_plot, x='years', y='ratio', barmode='stack',
color='mcname',
color_discrete_sequence= px.colors.diverging.Portland,
title='雑誌別の合計作品数')
show_fig(fig)
2.3.3. 雑誌別・年代別の合計作品数¶
col_count = 'cname'
# 10年単位で区切ったyearsを追加
df = add_years_to_df(df, 10)
# mcname, yearsで集計
df_plot = \
df.groupby(['mcname', 'years'])[col_count].\
nunique().reset_index()
# years単位で集計してdf_plotにカラムを追加
df_tmp = df_plot.groupby('years')[col_count].sum().reset_index(
name='years_total')
df_plot = pd.merge(df_plot, df_tmp, how='left', on='years')
# years合計あたりの比率を計算
df_plot['ratio'] = df_plot[col_count] / df_plot['years_total']
fig = px.bar(
df_plot, x='years', y='ratio', color='mcname',
color_discrete_sequence= px.colors.diverging.Portland,
barmode='stack', title='雑誌別・年代別の合計作品数')
show_fig(fig)
fig = px.bar(
df_plot, x='years', y='ratio', color='mcname',
color_discrete_sequence= px.colors.diverging.Portland,
barmode='group', title='雑誌別・年代別の合計作品数')
show_fig(fig)
2.3.4. 雑誌別の合計作者数¶
col_count = 'creator'
df_plot = \
df.groupby('mcname')[col_count].nunique().reset_index()
df_plot['ratio'] = df_plot[col_count] / df_plot[col_count].sum()
df_plot['years'] = '全期間'
fig = px.bar(
df_plot, x='years', y='ratio', barmode='stack',
color_discrete_sequence= px.colors.diverging.Portland,
color='mcname', title='雑誌別の合計作者数')
show_fig(fig)
2.3.5. 雑誌別・年代別の合計作者数¶
col_count = 'creator'
# 10年単位で区切ったyearsを追加
df = add_years_to_df(df, 10)
# mcname, yearsで集計
df_plot = \
df.groupby(['mcname', 'years'])[col_count].\
nunique().reset_index()
# years単位で集計してdf_plotにカラムを追加
df_tmp = df_plot.groupby('years')[col_count].sum().reset_index(
name='years_total')
df_plot = pd.merge(df_plot, df_tmp, how='left', on='years')
# years合計あたりの比率を計算
df_plot['ratio'] = df_plot[col_count] / df_plot['years_total']
fig = px.bar(
df_plot, x='years', y='ratio', color='mcname',
color_discrete_sequence= px.colors.diverging.Portland,
barmode='stack', title='雑誌別・年代別の合計作者数')
show_fig(fig)
fig = px.bar(
df_plot, x='years', y='ratio', color='mcname',
color_discrete_sequence= px.colors.diverging.Portland,
barmode='group', title='雑誌別・年代別の合計作者数')
show_fig(fig)